/*
 * @Author: lll 1280388921@qq.com
 * @Date: 2025-06-18 22:03:28
 * @LastEditors: lll 1280388921@qq.com
 * @LastEditTime: 2025-06-18 22:15:33
 * @FilePath: /lesson42/2.tcp/Init.hpp
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
#pragma once

#include <iostream>
#include <fstream>
#include <string>
#include <unordered_map>
#include "log.hpp"


using namespace std;

string dictname = "./dict.txt";
string sep = ":";

static bool Slipt(string s, string *p1, string *p2)
{
    auto pos=s.find(sep);
    if(pos==string::npos)
    {
        return false;
    }
    *p1=s.substr(0,pos);
    *p2=s.substr(pos+1);
    return true;
}

class Init
{
public:
    Init()
    {
        ifstream in(dictname);
        if (!in.is_open())
        {
            lg(Fatal, "ifstream open %s error", dictname.c_str());
            exit(1);
        }

        string line;
        while (getline(in, line))
        {
            string p1, p2;
            Slipt(line, &p1, &p2);
            dict.insert({p1, p2});
        }

        in.close();
    }
    string Translation(const string &key)
    {
        auto it=dict.find(key);
        if(it==dict.end())
        {
            return "Unknown";
        }
        else
        {
            return it->second;
        }
    }

private:
    unordered_map<string, string> dict;
};